<template>
  <div style="width: 70%;margin-top: 5%">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="新的密码" prop="password">
        <el-input show-password type="password" v-model="ruleForm.password" autocomplete="off" ></el-input>
      
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input show-password type="password" v-model="ruleForm.checkPass" autocomplete="off">
           </el-input>
                   
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
    
<script>
import { Message } from "element-ui";
export default {
  name: "countcontrol",

  data() {

    //密码的位数要求
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (value.length < 6) {
          callback(new Error("密码长度不能低于6"))
        }
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    //验证二次输入
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
   

      ruleForm: {
        
        password: '',
        checkPass: '',

      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],

      }
    };
  },
   methods: {
  
    //初始化数据
    data: function () {
      var tokenObject = this.$Storage.getItemForLs(this.$TestData.TOKEN_OBJECT_KEY);
    
      this.$Request
        .fetch("/api/rentuser/user/" + tokenObject.aud, {})
        .then((result) => {
          this.ruleForm = result;
          //清空获得的原密码
          this.ruleForm.password = '';

        })
        .catch(() => { });
    },
    //修改密码
    submitForm(formName) {

      this.$refs[formName].validate((valid) => {
        if (valid) {
          //删除多余部分
          delete this.ruleForm.checkPass;
          
          this.$Request
            .fetch_("/api/rentuser/password", 'put', this.ruleForm)
            .then((result) => {
              if (result.status == this.$Request.OK_CODE) {
                //成功后跳转登录页面
                this.$router.push('/login')
                Message({
                    message: result.message,
                    type: "success",
                    duration: 5 * 1000,
                  });
               
              } else {
                Message({
                    message: result.message,
                    type: "error",
                    duration: 5 * 1000,
                  });
              }
            })
            .catch((error) => {
              this.$router.push('/login')
            });
        }
      });
    },
    resetForm(formName) {
      //重置表单
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.data();

  },

}
</script>
    
<style scoped>

</style>
